<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category ArticleService.php
 * @Depiction:
 * Author: hwy
 * Date: 2021/2/20  16:53
 * +--------------------------------------------------------+
 */

namespace App\Services;


use App\Exceptions\ResponseException;
use App\Models\Article;
use App\Models\Category;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class ArticleService extends BaseService
{
    protected $article;
    protected $category;

    public function __construct()
    {
        $this->article  = new Article();
        $this->category = new Category();
    }

    /**
     * @param $params 获取的id
     * @return mixed
     * @Depiction 获取指定id数据
     * @Author hwy
     * @Date  2020/9/17  16:19
     */
    public function getOne($params)
    {
        return $this->article->find($params);
    }

    /**
     * @param $params
     * @param array $with
     * @return Builder|Builder[]|Collection|Model|null
     * @Depiction 获取指定id数据-带关联关系
     * @Author hwy
     * @Date 2021/5/27  15:54
     */
    public function getOneWith($params, array $with)
    {
        return $this->article->with($with)->find($params);
    }

    /**
     * @param $params 分页参数
     * @return mixed
     * @Depiction 获取分页数据
     * @Author hwy
     * @Date  2020/9/17  16:21
     */
    public function getList($params)
    {
        $query   = $this->article->query();
        $perPage = $this->perPage($params);
        $query   = $this->paramsSet($params, $query);
        if (!empty($params['category'])) {
            $query = $query->where('cid', $params['category']);
        }

        $list = $query->with('has_one_category', 'tags')->orderBy('order', 'asc')->orderBy('id',
            'desc')->paginate($perPage)->toArray();

        return return_success_msg(ResponseException::REQUEST_SUCCESS_MESSAGE, $list);
    }

    /**
     * @param $params
     * @return array
     * @throws \Exception
     * @Depiction
     * @Author hwy
     * @Date 2021/5/27  15:56
     */
    public function addData($params)
    {
        $add_data                 = $params->input();
        $add_data['status']       = $this->commonSetStatusValue($params->status);
        $add_data['content_html'] = $add_data['editormd_id-html-code'];

        DB::beginTransaction();
        try {
            $add = $this->article->create($add_data);
            if ($add && !empty($params->get('tags'))) {
                $add->tags()->sync($params->get('tags'));
            }

        } catch (\Exception $e) {
            DB::rollBack();
            return return_error_msg(exception_msg($e->getMessage()));
        }


        if ($add) {
            DB::commit();
            return return_success_msg(ResponseException::SUCCESS_ADD_MESSAGE);
        } else {
            return return_error_msg(ResponseException::ERROR_ADD_MESSAGE);
        }
    }

    /**
     * @param $params
     * @param $id
     * @return array
     * @throws \Exception
     * @Depiction
     * @Author hwy
     * @Date 2021/5/27  15:56
     */
    public function editData($params, $id)
    {
        $data = $this->getOne($id);
        if (empty($data)) {
            return return_error_msg(ResponseException::DATA_NOT_EXIST_MESSAGE);
        }
        $edit_data = $params->input();
        empty($params->status) ? $edit_data['status'] = 2 : $params->status;

        if (!empty($edit_data['editormd_id-html-code'])) {
            $add_data['content_html'] = $edit_data['editormd_id-html-code'];
        }

        DB::beginTransaction();
        try {
            $edit = $data->update($edit_data);
            if ($edit) {
                $data->tags()->sync($params->get('tags', []));
            }
        } catch (\Exception $e) {
            DB::rollBack();
            return return_error_msg($e->getMessage());
        }
        if ($edit) {
            DB::commit();
            return return_success_msg(ResponseException::SUCCESS_EDIT_MESSAGE);
        } else {
            return return_error_msg(ResponseException::ERROR_EDIT_MESSAGE);
        }

    }

    /**
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy
     * @Date 2021/5/27  15:56
     */
    public function getRead($params)
    {
        $data = $this->article->where('id', $params)->first();

        return $data;
    }

    /**
     * @param $ids
     * @return array
     * @throws \Exception
     * @Depiction
     * @Author hwy
     * @Date 2021/5/27  15:56
     */
    public function delData($ids)
    {
        if (is_string($ids)) {
            $ids = [$ids];
        }
        $data = $this->article->query()->whereIn('id', $ids)->get();
        if (empty($data)) {
            return return_error_msg(ResponseException::DATA_NOT_EXIST_MESSAGE);
        }
        DB::beginTransaction();
        foreach ($data as $value) {
            try {
                //清除中间表数据
                $value->tags()->sync([]);
                $del = $value->delete();
            } catch (\Exception $e) {
                DB::rollBack();
                return return_error_msg($e->getMessage());
            }
        }

        if ($del) {
            DB::commit();
            return return_success_msg(ResponseException::SUCCESS_DELETE_MESSAGE);
        } else {
            return return_error_msg(ResponseException::ERROR_DELETE_MESSAGE);
        }

    }

}
